package com.rf.richfitwheel.admin.sys.util;


import org.springframework.beans.factory.annotation.Value;

import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.Date;
import java.util.Properties;


/**
 * 发送邮件工具类
 * @author jiaozhongcai
 *
 */
public class EmailUtil {

	private String username = null; // 邮箱用户名
	private String password = null; // 邮箱密码
	
	
	/**
	 * 格式类型，如 text/html;charset=gbk
	 */
	private String type; 
	
	/**
	 *  发送人
	 */
	private String from;  
	
	/**
	 * 标题
	 */
	private String subject; 
	
	/**
	 * 内容
	 */
	private String text; 
	
	/**
	 * 接收人，多个接收人用逗号分隔
	 */
	private String recipient;  
	
	/**
	 * 发送时间
	 */
	private String datetime;   


	public String getFrom() {
		return from;
	}


	public void setFrom(String from) {
		this.from = from;
	}


	public String getSubject() {
		return subject;
	}


	public void setSubject(String subject) {
		this.subject = subject;
	}


	public String getText() {
		return text;
	}


	public void setText(String text) {
		this.text = text;
	}


	public String getRecipient() {
		return recipient;
	}


	public void setRecipient(String recipient) {
		this.recipient = recipient;
	}


	public String getDatetime() {
		return datetime;
	}


	public void setDatetime(String datetime) {
		this.datetime = datetime;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}
	


	/**
	* 传入自己的邮箱用户名和密码
	* @param username
	* @param password
	*/
	public EmailUtil(String username, String password) {
		this.username = username;
		this.password = password;
	}
	/**
	 * 发送邮件的构造器
	 * @param type  格式类型
	 * @param from 发送人
	 * @param subject 标题
	 * @param text 内容
	 * @param recipient  接收人，多个接收人用逗号分隔
	 * @param datetime 时间
	 */
	public EmailUtil(String type, String from, String subject, String text, String recipient, String datetime) {
		this.type = type;
		this.from = from;
		this.subject = subject;
		this.text = text;
		this.recipient = recipient;
		this.datetime = datetime;
	}
	
	public EmailUtil() {
		super();
	}


	/**
	* 创建连接会话
	* @param props
	* @return
	*/
	public Session createSession(Properties props) {

		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				// 返回用户名密码认证
				PasswordAuthentication pa = new PasswordAuthentication(username, password);
				return pa;
			}
		});
		return session;
	}


	/**
	* 发送邮件(需设置SMTP)
	* @throws Exception 
	*/
	public void sendMail(Properties props, EmailUtil sem) throws Exception {
		Session session = createSession(props);
		// 消息
		Message msg = new MimeMessage(session);
		msg.setFrom(InternetAddress.parse(MimeUtility.decodeText(sem.getFrom()))[0]);

		// TO为初级收件人，CC为邮件副本抄送，BCC应该是密秘抄送吧，客户信用只做简单发送邮件即可
		msg.setRecipients(RecipientType.TO, InternetAddress.parse(sem.getRecipient()));
		msg.setSubject(sem.getSubject());
		msg.setText(sem.getText());
		msg.setSentDate(new Date());

		// 发送消息
		Transport.send(msg);

	}

	@Value("${mail.smtp.host}")
	private String host ;
	@Value("${mail.smtp.protocol}")
	private String protocol ;
	@Value("${mail.smtp.port}")
	private String port ;
	@Value("${mail.smtp.auth}")
	private String auth ;

	/**
	* 获取SMTP默认配置
	* @return
	*/
	public static Properties getSMTP() {
		Properties p = new Properties();
		p.setProperty("mail.smtp.host", "10.27.130.244"); //服务地址
		p.setProperty("mail.smtp.protocol", "smtp"); //协议
		p.setProperty("mail.smtp.port", "25"); //端口
		p.setProperty("mail.smtp.auth", "true");
//		p.setProperty("mail.smtp.host", host); //服务地址
//		p.setProperty("mail.smtp.protocol", protocol); //协议
//		p.setProperty("mail.smtp.port", port); //端口
//		p.setProperty("mail.smtp.auth", auth);
		return p;
	}

	 
	//测试
	public static void main(String[] args) throws Exception {
		/**
		 * 默认情况下，在这里输入账号和密码，便可收信与发信
		 */ 
		EmailUtil eu = new EmailUtil("masiyuan@cnpc.com.cn", "./@MaSiy0104");
		 
		EmailUtil sem = new EmailUtil();
		/**
		 * 发件人邮箱
		 */
		sem.setFrom("masiyuan@cnpc.com.cn");
		/**
		 * 收件人邮箱
		 */
		sem.setRecipient("masiyuan@cnpc.com.cn");
		/**
		 * 标题
		 */
		sem.setSubject("客户信用风险提示");
		
		StringBuffer theMessage = new StringBuffer();
        theMessage.append("尊敬的: "); 
        theMessage.append("张三 \r\n ");
        theMessage.append("   您好：\n ");
        
		/**
		 * 内容
		 */
		sem.setText(theMessage.toString());
		/**
		 * 发送邮件
		 */
		eu.sendMail(EmailUtil.getSMTP(), sem);
		System.out.println("发送成功");
 
	}

}
